/*
 * Copyright 2017 The Cartographer Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "slam/sensor/landmark_data.h"

#include "slam/transform/transform.h"

namespace slam {
namespace sensor {

proto::LandmarkData ToProto(const LandmarkData& landmark_data) {
  proto::LandmarkData proto;
  proto.set_timestamp(common::ToUniversal(landmark_data.time));
  for (const auto& observation : landmark_data.landmark_observations) {
    auto* item = proto.add_landmark_observations();
    item->set_id(observation.id);
    *item->mutable_landmark_to_tracking_transform() =
        transform::ToProto(observation.landmark_to_tracking_transform);
    item->set_translation_weight(observation.translation_weight);
    item->set_rotation_weight(observation.rotation_weight);
  }
  return proto;
}

LandmarkData FromProto(const proto::LandmarkData& proto) {
  LandmarkData landmark_data;
  landmark_data.time = common::FromUniversal(proto.timestamp());
  for (const auto& item : proto.landmark_observations()) {
    landmark_data.landmark_observations.push_back({
        item.id(),
        transform::ToRigid3(item.landmark_to_tracking_transform()),
        item.translation_weight(),
        item.rotation_weight(),
    });
  }
  return landmark_data;
}

slam::mapping::proto::ReflectorDectorOptions CreateReflectorDectorOptions(
    common::LuaParameterDictionary* parameter_dictionary) {
  slam::mapping::proto::ReflectorDectorOptions options;
  if (parameter_dictionary->HasKey("reflector_type")) {
    options.set_reflector_type(parameter_dictionary->GetInt("reflector_type"));
  } else {
    options.set_reflector_type(0);
  }

  if (parameter_dictionary->HasKey("circle_fit_method")) {
    options.set_circle_fit_method(
        parameter_dictionary->GetInt("circle_fit_method"));
  } else {
    options.set_circle_fit_method(0);
  }

  if (parameter_dictionary->HasKey("reflector_length")) {
    options.set_reflector_length(
        parameter_dictionary->GetDouble("reflector_length"));
  } else {
    options.set_reflector_length(0.1);
  }

  if (parameter_dictionary->HasKey("length_error")) {
    options.set_length_error(parameter_dictionary->GetDouble("length_error"));
  } else {
    options.set_length_error(0.02);
  }

  if (parameter_dictionary->HasKey("reflector_radius")) {
    options.set_reflector_radius(
        parameter_dictionary->GetDouble("reflector_radius"));
  } else {
    options.set_reflector_radius(0.05);
  }

  if (parameter_dictionary->HasKey("radius_error")) {
    options.set_radius_error(parameter_dictionary->GetDouble("radius_error"));
  } else {
    options.set_radius_error(0.01);
  }

  if (parameter_dictionary->HasKey("min_line_split_dist")) {
    options.set_min_line_split_dist(
        parameter_dictionary->GetDouble("min_line_split_dist"));
  } else {
    options.set_min_line_split_dist(0.05);
  }

  if (parameter_dictionary->HasKey("line_outlier_dist")) {
    options.set_line_outlier_dist(
        parameter_dictionary->GetDouble("line_outlier_dist"));
  } else {
    options.set_line_outlier_dist(0.1);
  }

  if (parameter_dictionary->HasKey("min_reflector_counter_per_cluster")) {
    options.set_min_reflector_counter_per_cluster(
        parameter_dictionary->GetInt("min_reflector_counter_per_cluster"));
  } else {
    options.set_min_reflector_counter_per_cluster(5);
  }

  if (parameter_dictionary->HasKey("max_line_gap")) {
    options.set_max_line_gap(parameter_dictionary->GetDouble("max_line_gap"));
  } else {
    options.set_max_line_gap(0.1);
  }

  options.set_match_distance_threshold(
      parameter_dictionary->GetDouble("match_distance_threshold"));
  options.set_reflector_translation_weight(
      parameter_dictionary->GetDouble("reflector_translation_weight"));
  options.set_reflector_rotation_weight(
      parameter_dictionary->GetDouble("reflector_rotation_weight"));
  options.set_fusion_translation_weight(
      parameter_dictionary->GetDouble("fusion_translation_weight"));
  options.set_fusion_rotation_weight(
      parameter_dictionary->GetDouble("fusion_rotation_weight"));
  options.set_new_reflector_min_distance_threshold(
      parameter_dictionary->GetDouble("new_reflector_min_distance_threshold"));

  return options;
}

}  // namespace sensor
}  // namespace slam
